package com.naiterui.ehp.ps.sms.factory;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.ps.sms.enums.HuaweiStatusEnum;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.vo.MessageResponseVO;

/**
 * @author guoyongxiang
 * @date 2019-08-19 13:40
 * @since 1.0.0
 */
public class HuaweiSms {

    private static final Logger LOGGER = LoggerFactory.getLogger(HuaweiSms.class);

    public static MessageResponseVO singleSend(String phone, String channelNumber, String templateId, String[] datas, Integer platformId) throws SmsBusinessException {
        // 发送短信
        String result = HuaweiApi.singleSend(phone, channelNumber, templateId, datas, platformId);
        LOGGER.info("[发送短信，华为平台]--短信发送完成，手机号电话号码:[{}] 模板ID:[{}] 模板参数:[{}] 第三方平台返回结果:[{}]", phone, templateId, datas, result);
        MessageResponseVO messageResponseVO = processApiResult(result);
        return messageResponseVO;
    }

    private static MessageResponseVO processApiResult(String result) throws SmsBusinessException {
        // 处理api请求结果
        if (StringUtils.isEmpty(result)) {
            LOGGER.error("华为平台返回结果为空：[{}]", result);
            throw new SmsBusinessException(ExceptionCodes.SMS_PLATFORM_RETURN_NULL);
        }
        JSONObject resultJson = JSONObject.parseObject(result);
        MessageResponseVO responseVO = new MessageResponseVO();
        String statusCode = resultJson.getString("code");
        if (HuaweiStatusEnum.SMS_SUCCESS.code().equals(statusCode)) {
            responseVO.setStatusCode(String.valueOf(ExceptionCodes.SUCCESS));
            responseVO.setStatusMsg(HuaweiStatusEnum.SMS_SUCCESS.msg());
        } else {
            Integer covertCode = convertErrorCode(statusCode);
            if (covertCode != null) {
                LOGGER.error("华为平台返回状态码转换错误,statusCode：{}", statusCode);
                throw new SmsBusinessException(covertCode);
            } else {
                responseVO.setStatusCode(statusCode);
                responseVO.setStatusMsg(resultJson.getString("description"));
            }
        }

        return responseVO;
    }

    private static Integer convertErrorCode(String statusCode) {
        if (HuaweiStatusEnum.SMS_TEMPLAT_NOT_ACTIVE.code().equals(statusCode) || HuaweiStatusEnum.SMS_TEMPLAT_TYPE_ERROR.code().equals(statusCode)
                || HuaweiStatusEnum.SMS_TEMPLAT_TYPE_VERIFY_FAILED.code().equals(statusCode) || HuaweiStatusEnum.SMS_TEMPLAT_VAR_VERIFY_FAILED.code()
                                                                                                                                              .equals(statusCode)) {
            // 未找到对应的平台模板
            return ExceptionCodes.SMS_PLATFORM_TEMPLATE_NULL;
        } else if (HuaweiStatusEnum.SMS_SEND_BUSY.code().equals(statusCode)) {
            // 请求超过平台限制
            return ExceptionCodes.SMS_REQUEST_OVER_LIMIT;
        }

        return null;
    }

}
